package de.wbh.gwt.form.layouter.gwt.ui.view;

import javax.inject.Inject;

import com.google.common.collect.Lists;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SingleSelectionModel;

import de.wbh.gwt.form.layouter.gwt.ui.view.LoadConfigurationViewImpl.Resources.Style;
import de.wbh.gwt.form.layouter.shared.activity.LoadConfigurationView;

public class LoadConfigurationViewImpl extends Composite implements LoadConfigurationView {

	interface LoadConfigurationViewImplUiBinder extends UiBinder<Widget, LoadConfigurationViewImpl> {
	}

	public static interface Resources extends ClientBundle {

		static interface Style extends CssResource {
			String contentPanel();

			String headline();

			String panel();
		}

		@Source("loadPdfViewImpl.css")
		Style style();

	}

	private static LoadConfigurationViewImplUiBinder uiBinder = GWT.create(LoadConfigurationViewImplUiBinder.class);

	private Presenter presenter;

	@UiField(provided = true)
	Resources resources;

	private final Style style;

	@UiField(provided = true)
	CellList<String> cellList = new CellList<String>(new TextCell());

	SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();

	@Inject
	public LoadConfigurationViewImpl(Resources resources) {
		this.resources = resources;
		this.style = resources.style();
		this.style.ensureInjected();
		initWidget(uiBinder.createAndBindUi(this));
		this.cellList.setSelectionModel(this.selectionModel);
	}

	@UiHandler("useButton")
	void onUseButtonClicked(ClickEvent event) {
		if (this.presenter != null) {
			this.presenter.configurationSelected(this.selectionModel.getSelectedObject());
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void showConfigurations(Iterable<String> configs) {
		this.cellList.setRowData(Lists.newArrayList(configs));
	}

}
